"""
Case Type   : rowcount_big()函数
Case Name   : copy数据库后使用rowcount_big()函数
Create At   : 2025/07/30
Owner       : zym_2025
Description :
    1、建表并插入数据;
    2、copy数据库;
    3、执行rowcount_big();
    4、清理环境;
Expect      :
    1、建表成功,插入数据成功;
    3、返回0;
    4、清理环境成功;
History     :
"""

import os
import unittest
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

class RowcountBig(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info('--OpenGauss_Compatibility_Function_Rowcount_Big_Case0021 start--')
        self.constant = Constant()
        self.node = Node(node='PrimaryDbUser')
        self.comsh = CommonSH('PrimaryDbUser')
        self.db_name = 'db_d'
        self.table_name = 't_rowcount_0021'
        self.copy_file_path = os.path.join(macro.DB_INSTANCE_PATH, 'ds_t_rowcount0021.dat')

    def testRowcountBig(self):
        self.log.info('------创建D库并创建shark插件------')
        create_db = f"drop database if exists {self.db_name};create database {self.db_name} dbcompatibility 'D';"
        res = self.comsh.execut_db_sql(create_db)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res, '创建D库失败')
        create_ext = f"create extension shark;"
        res = self.comsh.execut_db_sql(create_ext, dbname=self.db_name)
        self.assertIn(self.constant.create_extension_success, res, '创建shark插件失败')

        text = '------step1:建表并插入数据; expect:成功------'
        self.log.info(text)
        create_sql = f'''drop table if exists {self.table_name};
                       create table {self.table_name} (id INT PRIMARY KEY, name VARCHAR(50));'''
        res = self.comsh.execut_db_sql(create_sql, dbname=self.db_name)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, res, '建表失败' + text)
        insert_sql = f"insert into {self.table_name} values(1,'zhh');"
        res = self.comsh.execut_db_sql(insert_sql, dbname=self.db_name)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res, '插入数据失败' + text)

        text = '------step2:copy数据库; expect:成功------'
        self.log.info(text)
        copy_sql = f"copy {self.table_name} to '{self.copy_file_path}';"
        res = self.comsh.execut_db_sql(copy_sql, dbname=self.db_name)
        self.assertIn(self.constant.copy_success_msg, res, 'copy数据库失败')

        text = '------step3:使用rowcount_big();expect:返回0------'
        self.log.info(text)
        sql = "select rowcount_big();"
        res = self.comsh.execut_db_sql(sql, dbname=self.db_name)
        self.assertTrue('0' in res.splitlines()[-2], 'rowcount_big()查询结果异常')

    def tearDown(self):
        text = '------step4:清理环境;expect:成功------'
        self.log.info(text)
        del_tab = f"drop table if exists {self.table_name};"
        res = self.comsh.execut_db_sql(del_tab, dbname=self.db_name)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, res, '删表失败')

        drop_sql = f"drop database {self.db_name};"
        res = self.comsh.execut_db_sql(drop_sql)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, res, '删除D库失败')

        cmd = f"rm -f {self.copy_file_path}"
        self.node.sh(cmd)
        self.log.info('--OpenGauss_Compatibility_Function_Rowcount_Big_Case0021 end--')